package com.qisi.kandroidshalon.leetcode;

import android.util.Log;

import com.qisi.kandroidshalon.leetcode.linklist.ListNode;

import java.util.List;

public class Util {

    public static final String TAG = "LeetCodeUtil";

    public static final void dumpArray(Integer[] array) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < array.length; i++) {
            sb.append(array[i]);
            if (i != array.length - 1) {
                sb.append(",");
            }
        }
        Log.i(TAG, sb.toString());
    }

    public static void dumpArray(int[] array) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < array.length; i++) {
            sb.append(array[i]);
            if (i != array.length - 1) {
                sb.append(",");
            }
        }
        Log.i(TAG, sb.toString());
    }


    public static final void dumpList(List<Integer> list) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            sb.append(list.get(i));
            if (i != list.size() - 1) {
                sb.append(",");
            }
        }
        Log.i(TAG, sb.toString());
    }

    /**
     * 交换元素
     *
     * @param array
     * @param i
     * @param j
     */
    public static void swap(Integer[] array, int i, int j) {
        int temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }

    /**
     * 求最大值
     *
     * @param array
     * @return
     */
    public static int max(Integer[] array) {
        int max = array[0];
        for (int i = 1; i < array.length; i++) {
            if (max < array[i]) {
                max = array[i];
            }
        }
        return max;
    }

    /**
     * 添加节点
     *
     * @param head
     * @param val
     * @return
     */
    public static void addNode(ListNode head, int val) {
        ListNode newNode = new ListNode(val);
        ListNode cur = head;
        while (cur != null) {
            if (cur.next == null) {
                break;
            }
            cur = cur.next;
        }
        cur.next = newNode;

    }

    public static void dumpNode(ListNode head) {
        StringBuilder sb = new StringBuilder();
        ListNode cur = head;
        while (cur != null) {
            sb.append(cur.value + ",");
            cur = cur.next;
        }
        Log.i(TAG, "KNode=" + sb.toString());
    }
}
